<%@ WebHandler Language="C#" Class="GetImage" %>

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using MySql.Data.Types;
using MySql.Data;
using System.Web;
using System.Data.OleDb;
using System.Data;
using FutureOpen.Core;
using FutureOpen.Core.func;
using FutureOpen.Core.func.db;
using FutureOpen.Core.func.sql;

public class GetImage : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string id = (string)context.Request["id"];
        string type = (string)context.Request["t"];
        
        Bitmap bm = null;
        Image image = null;
        if (!string.IsNullOrEmpty(id))
        {
            MemoryStream stream = new MemoryStream();                  
            try
            {
                DataRow dr = PicProvider.GetPic(Convert.ToInt32(id));
               
                if (dr != null)
                {
                    byte[] blob = (byte[])dr["pic_content"];

                    if (blob.Length > 0)
                    {
                        try
                        {
                            stream.Write(blob, 0, blob.Length);
                            image = Image.FromStream(stream);
                        }
                        catch (Exception) 
                        {
                            image = Image.FromFile(context.Request.PhysicalApplicationPath + "Images\\image_nopic.jpg");                                                     
                        }
                    }
                    else
                    {
                        image = Image.FromFile(context.Request.PhysicalApplicationPath + "Images\\image_nopic.jpg");                         
                    }
                }
                else
                {
                    image = Image.FromFile(context.Request.PhysicalApplicationPath + "Images\\image_nopic.jpg");
                }
                if (!string.IsNullOrEmpty(type))
                {
                    if (type == "m")
                    {
                        image = image.GetThumbnailImage(120, 120, null, IntPtr.Zero);
                    }
                    else if (type == "l")
                    {
                        image = image.GetThumbnailImage(180, 180, null, IntPtr.Zero);
                    }
                    else if (type == "s")
                    {
                        image = image.GetThumbnailImage(45, 45, null, IntPtr.Zero);
                    }
                    else
                    {
                        image = image.GetThumbnailImage(60, 60, null, IntPtr.Zero);
                    }
                    
                }
                else
                {
                    image = image.GetThumbnailImage(60, 60, null, IntPtr.Zero);
                }

                context.Response.ContentType = "image/jpeg";
                image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            }
            finally
            {
                if (image != null)
                {
                    image.Dispose();
                }
                if (bm != null)
                {
                    bm.Dispose();
                }

                stream.Close();
            }
        }
        else
        {
            try
            {
                image = Image.FromFile(context.Request.PhysicalApplicationPath + "Images\\image_nopic.jpg");
                context.Response.ContentType = "image/jpeg";
                image = image.GetThumbnailImage(60, 60, null, IntPtr.Zero);
                image.Save(context.Response.OutputStream, ImageFormat.Jpeg);                
            }
            finally
            {
                if (image != null)
                {
                    image.Dispose();
                }
                if (bm != null)
                {
                    bm.Dispose();
                }
            }
           
        }
    }

    public bool IsReusable
    {
        get
        {
            return true;
        }
    }
}